package com.nsu.evaluation.service.impl;

import com.nsu.evaluation.common.ServerResponse;
import com.nsu.evaluation.dao.StudentScoreMapper;
import com.nsu.evaluation.pojo.StudentScore;
import com.nsu.evaluation.pojo.StudentScoreExample;
import com.nsu.evaluation.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ScoreServiceImpl implements ScoreService {

    @Autowired
    private StudentScoreMapper studentScoreMapper;

    /**
     * 新增学生成绩
     * @param studentScore
     * @return
     */
    @Override
    public ServerResponse<StudentScore> addStudentScore(StudentScore studentScore){
        int rowCount = studentScoreMapper.insert(studentScore);
        if(rowCount > 0){
            return ServerResponse.createBySuccess("新增成绩成功",studentScore);
        }
        return ServerResponse.createByErrorMessage("新增成绩失败");
    }

    /**
     * 删除学生成绩
     * @param studentScoreId
     * @return
     */
    @Override
    public ServerResponse deleteStudentScore(String studentScoreId){
        int rowCount = studentScoreMapper.deleteByPrimaryKey(studentScoreId);
        if(rowCount > 0){
            return ServerResponse.createBySuccess("删除成绩成功",studentScoreId);
        }
        return ServerResponse.createByErrorMessage("删除成绩失败");
    }

    /**
     * 修改学生成绩
     * @param studentScore
     * @return
     */
    @Override
    public ServerResponse updateStudentScoreByPrimaryKey(StudentScore studentScore){
        int rowCount = studentScoreMapper.updateByPrimaryKey(studentScore);
        if(rowCount > 0){
            return ServerResponse.createBySuccess("修改成绩成功",studentScore);
        }
        return ServerResponse.createByErrorMessage("修改成绩失败");
    }

    /**
     * 根据学年学期或者学生id查询成绩
     * @param studentScore
     * @return
     */
    @Override
    public ServerResponse<List<StudentScore>> selectStudentScoreByPrimaryKey(StudentScore studentScore) {
        StudentScoreExample studentScoreExample = new StudentScoreExample();
        StudentScoreExample.Criteria criteria = studentScoreExample.createCriteria();
        //学年学期查询
        if (null != studentScore.getAcademicSemesterId() && !studentScore.getAcademicSemesterId().equals("")) {
            criteria.andAcademicSemesterIdEqualTo(studentScore.getAcademicSemesterId());
        }
        //学生id查询
        if(null != studentScore.getStudentId() && !studentScore.getStudentId().equals("")){
            criteria.andStudentIdEqualTo(studentScore.getStudentId());
        }
        List<StudentScore> studentScoreList = studentScoreMapper.selectByExample(studentScoreExample);
        return ServerResponse.createBySuccess("查询成绩成功",studentScoreList);
    }

    /**
     * 获取所有的成绩列表
     * @return
     */
    @Override
    public ServerResponse<List<StudentScore>> getAllScoreList(){
        StudentScoreExample studentScoreExample = new StudentScoreExample();
        List<StudentScore> studentScoreList = studentScoreMapper.selectByExample(studentScoreExample);
        if(studentScoreList != null && studentScoreList.size()>0){
            return ServerResponse.createBySuccess("获取成绩列表成功",studentScoreList);
        }
        return ServerResponse.createByErrorMessage("获取成绩列表失败");
    }
}
